package org.example.zhihuan.services;

import org.example.zhihuan.services.projectException.GoodsExistException;
import org.example.zhihuan.services.projectException.OrderExistException;
import org.example.zhihuan.services.projectException.UserIsSavedException;
import org.example.zhihuan.util.Db;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//潘思航
public class Order {
    private static int WAIT = 0;
    private static int SELL_PASS = 2;
    private static int SELL_FAIL = 3;


    private int orderId;                //订单id
    private String orderNumber;         //订单号
    private Date starTime;              //订单开始时间
    private Date endTime;               //订单结束时间
    private int sellerId;
    private int buyerId;
    private List<OrderRecord> sellGoodsList;               //卖方列表
    private List<OrderRecord> buyGoodsList;                //买方列表

    private int orderState;             //订单状态



    private boolean isSaved = false;

    /**
     * Order GetById
     *
     * @param orderId
     * @return
     */
    public static Order getOrderById(int orderId) {
        Order order = new Order();
        Db.read("select * from orderdetail where order_id = " + orderId, set -> {
            if (set.next()) {
                order.orderNumber = set.getString("order_number");
                order.starTime = set.getTimestamp("order_start_time");
                order.endTime = set.getTimestamp("order_end_time");
                order.sellerId = set.getInt("order_seller_id");
                order.buyerId = set.getInt("order_buyer_Id");
                order.orderState = set.getInt("order_state");
                order.isSaved = true;
            }
            return null;
        });
        return order;
    }

    /**
     * Order id存在
     *
     * @param orderId
     * @return
     */
    public static boolean orderIdExist(int orderId) {
        return Db.read("select 1 from orderdetail where order_id = " + orderId, ResultSet::next);
    }

    /**
     * 持久化
     */
    public void save() {
        SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        StringBuilder sb = new StringBuilder();
        if (isSaved) {
            sb.append("update orderdetail set ")
                    .append(" order_number = '").append(orderId).append("',")
                    .append(" order_start_time = '").append(fm.format(starTime)).append("',")
                    .append(" order_end_time = '").append(fm.format(endTime)).append("',")
                    .append(" order_seller_id = ").append(sellerId).append(",")
                    .append(" order_buyer_Id = ").append(buyerId).append(",")
                    .append(" order_state = ").append(orderState).append(",")
                    .append(" where order_id = ").append(orderId);
        } else {
            sb.append("insert into orderdetail (order_number,order_start_time,order_end_time,order_seller_id," +
                    "order_buyer_Id,order_state) values" +
                    " ('").append(orderNumber).append(",'").append(fm.format(starTime)).append("','")
                    .append(fm.format(endTime)).append("',").append(sellerId).append(",")
                    .append(buyerId).append(",").append(orderState).append(")");
            isSaved = true;
        }
        Db.execute(sb.toString());
    }

    /**
     * 创建订单
     * @param sellerId
     * @param buyerId
     * @param sellGoodsList
     * @param buyGoodsList
     * @return
     */
    public static boolean orderCreate(int sellerId, int buyerId,List<Integer> sellGoodsList,List<Integer> buyGoodsList) {
        if(!User.userIsSavedById(sellerId)){
            throw new UserIsSavedException("卖家用户id不存在");
        }
        if(!User.userIsSavedById(buyerId)){
            throw new UserIsSavedException("买家用户id不存在");
        }

        Order or = new Order();
        or.buyGoodsList = new ArrayList<>();
        for (Integer goodsId : buyGoodsList) {
            OrderRecord record = new OrderRecord();
            record.setGoodsId(goodsId);
            record.setUserId(buyerId);
            or.buyGoodsList.add(record);
        }
        or.sellGoodsList = new ArrayList<>();
        for (Integer goodsId : sellGoodsList) {
            OrderRecord record = new OrderRecord();
            record.setGoodsId(goodsId);
            record.setUserId(buyerId);
            or.sellGoodsList.add(record);
        }
        or.sellerId = sellerId;
        or.buyerId = buyerId;
        or.orderState = WAIT;

        or.save();
        return true;
    }

    /**
     * order交易
     *
     * @param deal
     */
    public void orderDeal(boolean deal,int orderId) {
        if (deal) {
            this.orderId = orderId;
            this.orderState = SELL_PASS;
        } else {
            this.orderId = orderId;
            this.orderState = SELL_FAIL;
        }
        this.endTime = new Date();
        this.save();
    }





}
